package com.ludomancy.archeologyrpg.entities;

import java.awt.Graphics;
import java.awt.Point;
import com.ludomancy.archeologyrpg.Game;
import com.ludomancy.archeologyrpg.GameTheme;
import com.ludomancy.archeologyrpg.Entity;

public class Fungus extends Entity
{
	private static final int SPEED = 2;
	
	private static final int eatMax = Game.discretize(2000);
	private int eatTime = 0;
	
	private static final int sleepMax = Game.discretize(4000);
	private int sleepTime = 0;
	
	
	private Nest nest;
	
	private static final int STATE_EATING = 1;
	private static final int STATE_SEEKING_FOOD = 2;
	private static final int STATE_SEEKING_SHELTER = 3;
	private static final int STATE_SLEEPING = 4;
	private static final int STATE_CONFUSED = 5;
	private int state = STATE_SEEKING_FOOD;
	
	private Swamp currentFood = null;
	private double foodNeed = 1; 
	
	public Fungus(Point p, Nest nest)
	{
		super(p);
		this.nest = nest;
	}
	
	public String getInfo()
	{
		return "A complex, local lifeform!";
	}
	
	public void paint(Graphics g, int x, int y)
	{
		paintDefaultIcon(g, ALIEN_FUNGUS, x, y);
	}
	
	public void die()
	{
		dead = true;
	}
	
	public void paintInfo(Graphics g)
	{
		g.setColor(GameTheme.theme.getDefaultTextColor());
		g.setFont(GameTheme.theme.tinyFont);
	}

	public void update(boolean running, boolean activated)
	{
		if(running)
		{
			switch(state)
			{
				case STATE_SEEKING_FOOD:
					seekFood(false);
					break;
				case STATE_EATING:
					eat(false);
					break;
				case STATE_SEEKING_SHELTER:
					seekShelter(false);
					break;
				case STATE_SLEEPING:
					sleep(false);
					break;
			}
		}
	}

	private void seekShelter(boolean init)
	{
		if(init)
		{
			state = STATE_SEEKING_SHELTER;
			return;
		}
		
		Entity closestShelter = nest;
		// check for target validity
		if(closestShelter == null || closestShelter.dead)
		{
			// AAAAH!!! NOW WHAT?!?!?
			beConfused(true);
		}
		if(moveTowards(closestShelter))
		{
			sleep(true);
		}
	}

	private void sleep(boolean init)
	{
		if(init)
		{
			state = STATE_SLEEPING;
			sleepTime = 0;
			return;
		}
		sleepTime++;
		if(sleepTime >= sleepMax)
		{
			foodNeed = 1;
			seekFood(true);
		}
	}

	private void beConfused(boolean init)
	{
		if(init)
		{
			state = STATE_CONFUSED;
			return;
		}
	}

	private void eat(boolean init)
	{
		if(init)
		{
			state = STATE_EATING;
			eatTime = 0;
			return;
		}

		if(currentFood != null)
		{
			eatTime++;
			if(eatTime >= eatMax)
			{
				currentFood.loseHealth(10);
				foodNeed = 0;
			}

			if(currentFood.health <= 20 || foodNeed == 0)
			{
				seekShelter(true);
			}
		}
	}
	
	private void seekFood(boolean init)
	{
		Entity closestSwamp = Game.game.getClosestEntity(new Point(px, py), Swamp.class);
		// check for target validity
		if(closestSwamp == null)
		{
			seekShelter(true);
		}
		if(moveTowards(closestSwamp))
		{
			currentFood = (Swamp)closestSwamp;
			eat(true);
		}
	}

	public String getActionVerb()
	{
		return "Tag";
	}
	
	private boolean moveTowards(Entity target)
	{
		if(target != null)
		{
			if(Game.dist(new Point(px, py), target) > Game.ENTITY_TILE_SIZE)
			{
				int dx = target.px - px > 0?SPEED:-SPEED;
				int dy = target.py - py > 0?SPEED:-SPEED;						
				if (px != target.px) 
				{
					px += dx;
					// do not move farther than the target 
					if ((dx > 0 && px > target.px) || (dx < 0 && px < target.px))
						px = target.px; 					
				}
				if (py != target.py) 
				{
					py += dy;
					// do not move farther than the target 
					if ((dy > 0 && py > target.py) || (dy < 0 && py < target.py))
						py = target.py; 					
				}
			}
			else
			{
				return true;
			}
		}
		return false;

	}

}
